package com.briup.pts.web.controller;

import com.briup.pts.config.response.Result;
import com.briup.pts.domain.bean.Animal;
import com.briup.pts.domain.vo.AnimalVO;
import com.briup.pts.service.AnimalService;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequestMapping("/animal")
@RequiredArgsConstructor
public class AnimalController {

    private final AnimalService animalService;

    @GetMapping("/query")
    public Result<PageInfo<AnimalVO>> pageQuery(
            @Min(value = 1, message = "当前页码不能小于1") Integer pageNum,
            @Min(value = 1, message = "页面不能小于1") Integer pageSize,
            String aHealthy, String aGender
    ){
        PageInfo<AnimalVO> pageInfo = animalService
                .pageQuery(pageNum, pageSize, aHealthy, aGender);
        return Result.success(pageInfo);
    }

    @GetMapping("/findByAnimalId")
    public Result<AnimalVO> queryById(String animalId){
        AnimalVO animalVO = animalService.queryById(animalId);
        return Result.success(animalVO);
    }

    @DeleteMapping("/deleteById/{animalId}")
    public Result<?> deleteById(@PathVariable String animalId){
        animalService.deleteById(animalId);
        return Result.success();
    }

    @DeleteMapping("/deleteByIdAll")
    public Result<?> deleteByIAll(@RequestBody
        @NotEmpty(message = "待删除动物的id必须指定")List<String> animalIds){
        animalService.batchDelete(animalIds);
        return Result.success();
    }

    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody @Valid Animal animal){
        animalService.saveOrUpdate(animal);
        return Result.success();
    }

    @GetMapping("/QRcode")
    public Result<?> qrcode(String animalId){
        animalService.qrCode(animalId);
        return Result.success();
    }
}
